<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');


/**
 * Call a function given in string format used by configuration set and use functions
 *
 * @param string $function The complete function to call
 * @param string $default The default value to pass to the function
 * @param string $key The key value to use for the input field
 */
if (!function_exists('call_config_func')) {

    function call_config_func($function, $function_parameter = null, $default = null, $key = null, $calltype = 'global', $node_org_id = NULL) {
        if (empty($function)) return;
        $ci = &get_instance();
        if (strpos($function, '::') !== false) {
            $class_method = explode('::', $function);
            $class_str = $class_method[0];
            $function = $class_method[1];
            $class = str_replace($ci->config->item('subclass_prefix'), '', strtolower($class_str));
            $ci->load->library($class);

            $function_name = $function;
            if (!empty($function_parameter)) {
                return call_user_func(array($ci->$class, $function_name), $function_parameter, $default, $key, $calltype, $node_org_id);
            } else {
                return call_user_func(array($ci->$class, $function_name), $default, $key, $calltype, $node_org_id);
            }
        } else {
            $function_name = $function;

            if (!function_exists($function_name)) {
                $ci->load->helper('cfg_parameters/' . $function_name);
            }

            if (!empty($function_parameter)) {
                return call_user_func($function_name, $function_parameter, $default, $key, $calltype, $node_org_id);
            } else {
                return call_user_func($function_name, $default, $key, $calltype, $node_org_id);
            }
        }
    }

}

// ------------------------------------------------------------------------

/**
 * Get the IP address of the client
 *
 * @access public
 */
if (!function_exists('get_ip_address')) {

    function get_ip_address() {
        if (isset($_SERVER)) {
            if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
                $ip = $_SERVER['HTTP_CLIENT_IP'];
            } else {
                $ip = $_SERVER['REMOTE_ADDR'];
            }
        } else {
            if (getenv('HTTP_X_FORWARDED_FOR')) {
                $ip = getenv('HTTP_X_FORWARDED_FOR');
            } elseif (getenv('HTTP_CLIENT_IP')) {
                $ip = getenv('HTTP_CLIENT_IP');
            } else {
                $ip = getenv('REMOTE_ADDR');
            }
        }

        return $ip;
    }

}
// ------------------------------------------------------------------------

/**
 * Encrypt password
 *
 * @access public
 * @param $plain the password
 * @return string encrypted password
 */
if (!function_exists('encrypt_password')) {

    function encrypt_password($plain) {
        $password = '';

        for ($i = 0; $i < 10; $i++) {
            $password .= mt_rand();
        }

        $salt = substr(md5($password), 0, 2);

        $password = md5($salt . $plain) . ':' . $salt;

        return $password;
    }

}
/**
 * Gets a configuration
 *
 * @access public
 * @param $key The key to the configuration
 * @return string configuration value
 */
if (!function_exists('config')) {

    function config($key) {
        $CI = & get_instance();
        $line = $CI->configuration->line($key);

        return $line;
    }

}
/**
 * Gets a configuration for PBX node
 *
 * @access public
 * @param $key The key to the configuration
 * @param $node_id The node id  to the configuration
 * @return string configuration value or NULL
 */
if (!function_exists('config_node')) {

    function config_node($key, $node_id) {
        $CI = & get_instance();
        $line = $CI->configuration->get_setting_node($key, $node_id);
        return $line;
    }

}
/**
 * Gets a configuration for organization
 *
 * @access public
 * @param $org_id The organzaion id  to the configuration
 * @return string configuration value or NULL
 */
if (!function_exists('config_org')) {

    function config_org($key, $org_id) {
        $CI = & get_instance();
        $line = $CI->configuration->get_setting_org($key, $org_id);
        return $line;
    }

}

/**
 * Get current lang id
 *
 * @access public
 * @return int
 */
if (!function_exists('lang_id')) {

    function lang_id() {
        $CI = & get_instance();
        $lang_id = $CI->lang->get_id();

        return $lang_id;
    }

}
/**
 * Get current lang code
 *
 * @access public
 * @return string
 */
if (!function_exists('lang_code')) {

    function lang_code() {
        $CI = & get_instance();
        $lang_code = $CI->lang->get_code();

        return $lang_code;
    }

}
   